package com.sinaapp.gavinzhang.GSpider;

import com.sinaapp.gavinzhang.GSpider.config.Constants;
import com.sinaapp.gavinzhang.GSpider.core.*;
import com.sinaapp.gavinzhang.GSpider.process.*;
import com.sinaapp.gavinzhang.GSpider.process.Process;

import java.util.*;
import java.util.regex.Pattern;

/**
 * 用于简单建立爬虫
 * Created by gavin on 15-8-1.
 */
public class GSpiderBuilder {
    /** 控制线程池数量*/
    private int controlExecutorNum;
    /** 任务线程池数量*/
    private int taskExecutorNum;
    /** 任务网址列表*/
    private AbstractGWebUrlList pendingList;
    /** 已经处理过的网址*/
    private AbstractGWebUrlList visitedList;
    /** 查找网址任务*/
    private AbstractProcess process;


    private HttpProcessorInterceptor interceptor;

    /** Task*/
    /** 符合要求的网址正则表达*/
    private List<Pattern> patterns;
    /** 设置处理目标函数*/
    private AbstractPageProcessor pageProcessor;

    public GSpiderBuilder() {
        controlExecutorNum = Constants.CONTROL_EXECUTOR_NUM;
        taskExecutorNum = Runtime.getRuntime().availableProcessors()* Constants.MULTI_THREAD_BASIC;
        }

    public GSpider build()
    {
        if ((patterns==null) || (patterns.size()==0)){
            throw new RuntimeException("patterns is empty");
        }
        if (controlExecutorNum < 1){
            throw new RuntimeException("controlExecutorNum is " + controlExecutorNum);
        }
        if (taskExecutorNum < 1){
            throw new RuntimeException("taskExecutorNum is " + taskExecutorNum);
        }
        if ((pendingList==null) || (pendingList.size()==0)){
            throw new RuntimeException("pedingList is empty");
        }
        if (visitedList == null){
            visitedList = new VisitedWebUrlList();
        }
        if (process == null){
            process = new Process();
        }
        if (pageProcessor == null){
            pageProcessor = new DefaultPageProcessor();
        }
        if (interceptor == null){
            interceptor = new DefaultHttpProcessorInterceptor();
        }
        return new GSpider(pendingList,visitedList,controlExecutorNum,taskExecutorNum,process,patterns,pageProcessor,interceptor);
    }


    public GSpiderBuilder addRegex(Pattern pattern)
    {
        if (patterns == null){
            patterns = new ArrayList<>();
        }
        this.patterns.add(pattern);
        return this;
    }

    public GSpiderBuilder addWebUrl(String url)
    {
        if (pendingList == null){
            pendingList = new PendingWebUrlList();
        }
        this.pendingList.add(url);
        return this;
    }

    public void setControlExecutorNum(int controlExecutorNum) {
        this.controlExecutorNum = controlExecutorNum;
    }

    public void setTaskExecutorNum(int taskExecutorNum) {
        this.taskExecutorNum = taskExecutorNum;
    }

    @Deprecated
    public void setCurrentUrlList(AbstractGWebUrlList currentUrlList) {
        this.pendingList = currentUrlList;
    }

    @Deprecated
    public void setFoundUrlList(AbstractGWebUrlList foundUrlList) {
        this.visitedList = foundUrlList;
    }

    public void setProcess(AbstractProcess process) {
        this.process = process;
    }

    @Deprecated
    public void setListP(List<Pattern> listP) {
        this.patterns = listP;
    }

    public void setPageProcessor(AbstractPageProcessor pageProcessor) {
        this.pageProcessor = pageProcessor;
    }

    @Deprecated
    public void setHttpProcessorInterceptor(HttpProcessorInterceptor httpProcessorInterceptor) {
        this.interceptor = httpProcessorInterceptor;
    }

    public void setPendingList(AbstractGWebUrlList pendingList) {
        this.pendingList = pendingList;
    }

    public void setVisitedList(AbstractGWebUrlList visitedList) {
        this.visitedList = visitedList;
    }

    public void setInterceptor(HttpProcessorInterceptor interceptor) {
        this.interceptor = interceptor;
    }

    public void setPatterns(List<Pattern> patterns) {
        this.patterns = patterns;
    }
}
